# Link flags to pull in dependencies
BINS = cargo \
cargo-build \
+ cargo-clean \
cargo-read-manifest \
cargo-rustc \
cargo-verify-project \
--- /dev/null
+#![crate_id="cargo-clean"]
+#![feature(phase)]
+
+extern crate cargo;
+
+#[phase(plugin, link)]
+extern crate hammer;
+
+#[phase(plugin, link)]
+extern crate log;
+
+extern crate serialize;
+
+use std::os;
+use cargo::ops;
+use cargo::{execute_main_without_stdin};
+use cargo::core::MultiShell;
+use cargo::util::{CliResult, CliError};
+use cargo::util::important_paths::find_project_manifest;
+
+#[deriving(PartialEq,Clone,Decodable,Encodable)]
+pub struct Options {
+ manifest_path: Option<String>
+}
+
+hammer_config!(Options)
+
+fn main() {
+ execute_main_without_stdin(execute);
+}
+
+fn execute(options: Options, _shell: &mut MultiShell) -> CliResult<Option<()>> {
+ debug!("executing; cmd=cargo-clean; args={}", os::args());
+
+ let root = match options.manifest_path {
+ Some(path) => Path::new(path),
+ None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
+ .map_err(|_| {
+ CliError::new("Could not find Cargo.toml in this \
+ directory or any parent directory",
+ 102)
+ }))
+ };
+
+ ops::clean(&root).map(|_| None).map_err(|err| {
+ CliError::from_boxed(err, 101)
+ })
+}
--- /dev/null
+use std::io::fs::{rmdir_recursive};
+use core::{SourceId};
+use util::{CargoResult, human, ChainError};
+use ops::{read_manifest};
+use std::io::{File};
+use util::toml::{project_layout};
+
+pub fn clean(path: &Path) -> CargoResult<()>
+{
+ let mut file = try!(File::open(path));
+ let data = try!(file.read_to_end());
+ let layout = project_layout(&path.dir_path());
+ let (manifest, _) = try!(read_manifest(data.as_slice(), layout, &SourceId::for_path(path)));
+ let build_dir = manifest.get_target_dir();
+
+ if build_dir.exists() {
+ return rmdir_recursive(build_dir).chain_error(|| human("Could not remove build directory"))
+ }
+
+ Ok(())
+}
+pub use self::cargo_clean::clean;
pub use self::cargo_compile::{compile, CompileOptions};
pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages};
pub use self::cargo_rustc::compile_targets;
+mod cargo_clean;
mod cargo_compile;
mod cargo_read_manifest;
mod cargo_rustc;
}
pub fn bin(&self, b: &str) -> Path {
- self.root.join("target").join(format!("{}{}", b, os::consts::EXE_SUFFIX))
+ self.build_dir().join(format!("{}{}", b, os::consts::EXE_SUFFIX))
+ }
+
+ pub fn build_dir(&self) -> Path {
+ self.root.join("target")
}
pub fn process<T: ToCStr>(&self, program: T) -> ProcessBuilder {
--- /dev/null
+use support::{project, execs, main_file, basic_bin_manifest};
+use hamcrest::{assert_that, existing_dir, is_not};
+
+fn setup() {
+}
+
+test!(cargo_clean_simple {
+ let p = project("foo")
+ .file("Cargo.toml", basic_bin_manifest("foo").as_slice())
+ .file("src/foo.rs", main_file(r#""i am foo""#, []).as_slice());
+
+ assert_that(p.cargo_process("cargo-build"), execs());
+ assert_that(&p.build_dir(), existing_dir());
+
+ assert_that(p.cargo_process("cargo-clean"), execs());
+ assert_that(&p.build_dir(), is_not(existing_dir()));
+})
)
)
+mod test_cargo_clean;
mod test_cargo_compile;
mod test_cargo_compile_git_deps;
mod test_cargo_compile_path_deps;